/*
 * Copyright (c) 2024 dragonSaberCaptain.All rights reserved.
 * 当前项目名:wisdom-parent
 * 当前模块名:common-tools
 * 当前文件的权限定名:com.wisdom.tools.system.SpringContextUtil
 * 当前文件的名称:SpringContextUtil.java
 * 当前文件的类名:SpringContextUtil
 * 上一次文件修改的日期时间:2024/1/19 下午1:31
 *
 */

package com.wisdom.tools.system;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class SpringContextUtil {
    private static ApplicationContext applicationContext;

    public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    public static void setApplicationContext(ConfigurableApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    /**
     * 获取applicationContext
     *
     * @author captain
     * @datetime 2022-06-14 13:11:49
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     */
    public static Object getBean(String name) {
        Object o = null;
        try {
            o = getApplicationContext().getBean(name);
        } catch (NoSuchBeanDefinitionException e) {
            log.info("获取bean失败!");
        }
        return o;
    }

    /**
     * 通过class获取Bean.
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }

    /**
     * 通过name获取 Bean.
     */
    public static <T> Map<String, T> getBeansOfType(Class<T> clazz) {
        return getApplicationContext().getBeansOfType(clazz);
    }

    /**
     * 获取配置文件配置项的值
     *
     * @param key 配置项key
     */
    public static String getEnvironmentProperty(String key) {
        return applicationContext.getEnvironment().getProperty(key);
    }

    /**
     * 获取spring.profiles.active
     */
    public static String[] getProfileActive() {
        return applicationContext.getEnvironment().getActiveProfiles();
    }

    /**
     * 获取激活的环境:
     * 1、local 本地环境
     * 2、dev 开发环境
     * 3、test 测试环境
     * 4、prod 生产环境
     */
    public static String getEnvironmentActive() {
        for (String activeProfile : getProfileActive()) {
            if ("local".equalsIgnoreCase(activeProfile)) return activeProfile;
            if ("dev".equalsIgnoreCase(activeProfile)) return activeProfile;
            if ("test".equalsIgnoreCase(activeProfile)) return activeProfile;
            if ("prod".equalsIgnoreCase(activeProfile)) return activeProfile;
        }
        return null;
    }
}